package com.buildings.activity;

/**
 * 选择图片界面
 */

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.buildings.R;
import com.buildings.adapter.ImgsAdapter;
import com.buildings.bean.FileTraversal;

import java.util.ArrayList;
import java.util.HashMap;

public class ImgsActivity extends Activity {
    private TextView tv_count;
    private LinearLayout btn_back;
    private LinearLayout btn_send;
    private int count;
    Bundle bundle;
    FileTraversal fileTraversal;
    GridView imgGridView;
    ImgsAdapter imgsAdapter;
    HashMap<Integer, ImageView> hashImage;
    ArrayList<String> filelist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.commactivity_photogrally);
        tv_count = (TextView) this.findViewById(R.id.tv_count);
        btn_back = (LinearLayout) this.findViewById(R.id.btn_back);
        btn_send = (LinearLayout) this.findViewById(R.id.btn_send);
        count = this.getIntent().getIntExtra("count", 0);

        tv_count.setText(getString(R.string.commactivity_activity_imgs_text, 0 + "", count
                + ""));
        imgGridView = (GridView) findViewById(R.id.gridView1);
        bundle = getIntent().getExtras();
        fileTraversal = bundle.getParcelable("data");
        imgsAdapter = new ImgsAdapter(this, fileTraversal.filecontent);
        imgGridView.setAdapter(imgsAdapter);
        hashImage = new HashMap<Integer, ImageView>();
        filelist = new ArrayList<String>();
        // imgGridView.setOnItemClickListener(this);
        btn_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
        btn_send.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (filelist == null || filelist.size() == 0) {
                    Toast.makeText(ImgsActivity.this, "请选择图片", Toast.LENGTH_SHORT).show();
                    return;
                }

                Intent intent = new Intent();
                intent.putStringArrayListExtra("files", filelist);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });

        imgGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                boolean checked = imgGridView.getCheckedItemPositions().get(position);
                String filapath = fileTraversal.filecontent.get(position);
                if (!checked) {
                    filelist.remove(filapath);
                    tv_count.setText(getString(R.string.commactivity_activity_imgs_text,
                            filelist.size() + "", count + ""));
                } else {
                    if (filelist.size() >= count) {
                        imgGridView.setItemChecked(position, false);
                        Toast.makeText(ImgsActivity.this,"图片超过限额"
                                ,Toast.LENGTH_SHORT).show();
                        return;
                    }
                    try {
                        filelist.add(filapath);
                        tv_count.setText(getString(R.string.commactivity_activity_imgs_text,
                                filelist.size() + "", count + ""));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        });
    }

    public void tobreak(View view) {
        finish();
    }

}
